package com.ddstation.onsitelaw.service;

import com.ddstation.common.util.IdGenerateUtil;
import com.ddstation.onsitelaw.entity.DtHealthDataWestPrescriptionForPublicEntity;
import com.ddstation.onsitelaw.mapper.DtHealthDataOutpatientMedicalMapper;
import com.ddstation.onsitelaw.mapper.DtHealthDataWestPrescriptionMapper;
import com.ddstation.onsitelaw.mapper.MaInvsBaseDrugMapper;
import com.ddstation.onsitelaw.model.DrugItems;
import com.ddstation.onsitelaw.model.PrescriptionModel;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import tk.mybatis.mapper.entity.Example;

import javax.inject.Inject;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
public class PrescriptionServiceImpl implements PrescriptionService {
    @Inject
    private DtHealthDataWestPrescriptionMapper dtHealthDataWestPrescriptionMapper;
    @Inject
    private MaInvsBaseDrugMapper baseDrugMapper;

    @Inject
    private DtHealthDataOutpatientMedicalMapper dtHealthDataOutpatientMedicalMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addPrescription(PrescriptionModel prescription) {
        List<DrugItems> items = prescription.getItemsList();
        String prescriptionId = IdGenerateUtil.idGenerateFromTime("04");
        if (!ObjectUtils.isEmpty(items)) {
            List<DtHealthDataWestPrescriptionForPublicEntity> prescriptionList = new ArrayList<>();

            for (int i = 0; i < items.size(); i++) {
                DtHealthDataWestPrescriptionForPublicEntity prescriptionForEntity = new DtHealthDataWestPrescriptionForPublicEntity();
                //复制属性给prescription对象（名字，字段类型要一致才）
                BeanUtils.copyProperties(prescription, prescriptionForEntity);
                //复制属性给药品明细item对象（名字，字段类型要一致才）
                BeanUtils.copyProperties(items.get(i), prescriptionForEntity);
                //entId对应处方表的prescription_Id
                prescriptionForEntity.setPrescriptionId(prescriptionId);
                prescriptionForEntity.setId(IdGenerateUtil.idGenerateFromTime("04"));
                prescriptionForEntity.setUpdateTm(new Date());
                prescriptionForEntity.setSourceType("04");
                Example example = new Example(DtHealthDataWestPrescriptionForPublicEntity.class);
                splitSql(prescriptionForEntity, example);
                //数据存在就更新，不存在就新增。
                List<DtHealthDataWestPrescriptionForPublicEntity> restList = dtHealthDataWestPrescriptionMapper.selectByExample(example);
                if (ObjectUtils.isEmpty(restList)) {
                    prescriptionList.add(prescriptionForEntity);
                } else {
                    dtHealthDataWestPrescriptionMapper.updateByExampleSelective(prescriptionForEntity, example);
                }
            }
            if (!ObjectUtils.isEmpty(prescriptionList)) {
                dtHealthDataWestPrescriptionMapper.insertList(prescriptionList);
            }
        }

        return prescriptionId;
    }


    /**
     * 组装sql执行条件
     *
     * @param prescriptionForEntity
     * @param example
     */
    private void splitSql(DtHealthDataWestPrescriptionForPublicEntity prescriptionForEntity, Example example) {
        //组装sql执行条件
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("medicalId", prescriptionForEntity.getMedicalId());
        criteria.andEqualTo("institutionId", prescriptionForEntity.getInstitutionId());
        criteria.andEqualTo("prescriptionId", prescriptionForEntity.getPrescriptionId());
    }

}